﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Input;

namespace GUILibrary
{
    public abstract class Component
    {

        /// <summary>
        /// Unique name of the component.
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Action that will be triggered if this component is clicked.
        /// </summary>
        public string Action
        {
            get;
            set;
        }

        /// <summary>
        /// The width and height of this component.
        /// </summary>
        public Vector2 Bounds
        {
            get;
            set;
        }

        /// <summary>
        /// Placement of the component on the screen.
        /// </summary>
        public Vector2 ScreenPosition
        {
            get;
            set;
        }

        /// <summary>
        /// Action is triggered or not.  Starts out false.
        /// </summary>
        [ContentSerializerIgnore]
        public bool Triggered
        {
            get;
            set;
        }

        /// <summary>
        /// The GUIScreen to which this component belongs.
        /// </summary>
        [ContentSerializerIgnore]
        public GUIScreen ParentScreen
        {
            get;
            set;
        }

        /// <summary>
        /// Whether or not this Component has been initialized.
        /// </summary>
        [ContentSerializerIgnore]
        public bool Initialized
        {
            get;
            set;
        }

        /// <summary>
        /// Whether or not the component is hovered by the mouse
        /// this frame.  Inits to false.
        /// </summary>
        [ContentSerializerIgnore]
        public bool Hovered
        {
            get;
            set;
        }

        /// <summary>
        /// Update the hover status of the Component.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void UpdateHover(float x, float y)
        {
            float leftX = ScreenPosition.X;
            float rightX = leftX + Bounds.X;
            float topY = ScreenPosition.Y;
            float bottomY = topY + Bounds.Y;

            bool horizontalCollision = (leftX <= x) && (rightX >= x);
            bool verticalCollision = (topY <= y) && (bottomY >= y);
            
            //collision has occured
            if (horizontalCollision && verticalCollision)
            {
                ChangeHover(true);
            }
            else
            {
                ChangeHover(false);
            }
        }

        /// <summary>
        /// Update the state of the component based on a mouse click.  The
        /// default behavior is to "trigger" the component.
        /// </summary>
        /// <param name="isClicked"></param>
        public virtual void UpdateMouseClick(GameInput input)
        {
            Triggered = input.IsLeftMouseButtonPressed() && Hovered;
        }

        /// <summary>
        /// Update the Hover state of the component.  Default behavior
        /// is to just set Hovered to true when the component is hovered.
        /// </summary>
        /// <param name="isHover"></param>
        protected virtual void ChangeHover(bool isHover)
        {
            Hovered = isHover;
        }

        public abstract void Draw(SpriteBatch batch);
        public abstract void Load(ContentManager cm);
    }
}
